package com.wyh.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //通过类名获取class对象
        Class<?> class1 = User.class;
        //通过全限定名获取class对象
        Class<?> class2 = Class.forName("com.wyh.reflection.User");
        //通过对象获取class对象
        User user = new User();
        Class<?> class3 = user.getClass();

        //获取无参构造方法并创建实例
        Constructor<?> declaredConstructor = class1.getDeclaredConstructor();
        User o = (User) declaredConstructor.newInstance();
        o.age=10;
        System.out.println(o);
        //获取有参构造
        Constructor<?> declaredConstructor1 = class2.getDeclaredConstructor(String.class,Integer.class);
        declaredConstructor1.setAccessible(true);
        User user1 = (User) declaredConstructor1.newInstance("张三", 13);
        System.out.println(user1);
    }

    public static void main2(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        User user = new User();
        Class<? extends User> aClass = user.getClass();
        Constructor<? extends User> declaredConstructor = aClass.getDeclaredConstructor();
        User user1 = declaredConstructor.newInstance();
        user1.age = 22;
        //获取所有公共方法，父类的也可以。
        Method method = aClass.getMethod("run");
        System.out.println(method.getName());
        method.invoke(user1);

        //获取所有声明的方法，不包括父类。
        Method method1 = aClass.getDeclaredMethod("run");
        Method method2 = aClass.getDeclaredMethod("getName");
        method2.setAccessible(true);
        //接收方法的返回值
        String invoke = (String) method2.invoke(user1);
        System.out.println(invoke);
        method1.setAccessible(true);
        method1.invoke(user1);
        System.out.println(user1.age);
    }

    public static void main1(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //通过类名创建实例
        Class<?> class1 = String.class;
//        通过对象创建实例
        String s = "null";
        Class<? extends String> aClass = s.getClass();
        Constructor<? extends String> declaredConstructor2 = aClass.getDeclaredConstructor(String.class);
        String s1 = declaredConstructor2.newInstance("dwa");
//        s1 += "14141";//在反射中不能+=，因为反射是绕过了编译器的编译阶段的，也就是平常都是编译器在帮忙（转为StringBuffer进行拼接）。
        System.out.println(s);
        Constructor<?> declaredConstructor = class1.getDeclaredConstructor();
        Object o = declaredConstructor.newInstance();
        Constructor<?> declaredConstructor1 = class1.getDeclaredConstructor(String.class);
        Object o1 = declaredConstructor1.newInstance("12123132");
        System.out.println(o1);
        System.out.println(o);
    }
}
